home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxicolor.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  11.8 KB  |  413 lines

  1. /* Copyright (C) 1992, 1995, 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxicolor.c,v 1.2 2000/09/19 19:00:37 lpd Exp $ */
  20. /* Color image rendering */
  21. #include "gx.h"
  22. #include "memory_.h"
  23. #include "gpcheck.h"
  24. #include "gserrors.h"
  25. #include "gxfixed.h"
  26. #include "gxfrac.h"
  27. #include "gxarith.h"
  28. #include "gxmatrix.h"
  29. #include "gsccolor.h"
  30. #include "gspaint.h"
  31. #include "gzstate.h"
  32. #include "gxdevice.h"
  33. #include "gxcmap.h"
  34. #include "gxdcconv.h"
  35. #include "gxdcolor.h"
  36. #include "gxistate.h"
  37. #include "gxdevmem.h"
  38. #include "gxcpath.h"
  39. #include "gximage.h"
  40.  
  41. typedef union {
  42.     byte v[GS_IMAGE_MAX_COLOR_COMPONENTS];
  43. #define BYTES_PER_BITS32 4
  44. #define BITS32_PER_COLOR_SAMPLES\
  45.   ((GS_IMAGE_MAX_COLOR_COMPONENTS + BYTES_PER_BITS32 - 1) / BYTES_PER_BITS32)
  46.     bits32 all[BITS32_PER_COLOR_SAMPLES];    /* for fast comparison */
  47. } color_samples;
  48.  
  49. /* ------ Strategy procedure ------ */
  50.  
  51. /* Check the prototype. */
  52. iclass_proc(gs_image_class_4_color);
  53.  
  54. private irender_proc(image_render_color);
  55. irender_proc_t
  56. gs_image_class_4_color(gx_image_enum * penum)
  57. {
  58.     if (penum->use_mask_color) {
  59.     /*
  60.      * Scale the mask colors to match the scaling of each sample to
  61.      * a full byte, and set up the quick-filter parameters.
  62.      */
  63.     int i;
  64.     color_samples mask, test;
  65.     bool exact = penum->spp <= BYTES_PER_BITS32;
  66.  
  67.     memset(&mask, 0, sizeof(mask));
  68.     memset(&test, 0, sizeof(test));
  69.     for (i = 0; i < penum->spp; ++i) {
  70.         byte v0, v1;
  71.         byte match = 0xff;
  72.  
  73.         gx_image_scale_mask_colors(penum, i);
  74.         v0 = (byte)penum->mask_color.values[2 * i];
  75.         v1 = (byte)penum->mask_color.values[2 * i + 1];
  76.         while ((v0 & match) != (v1 & match))
  77.         match <<= 1;
  78.         mask.v[i] = match;
  79.         test.v[i] = v0 & match;
  80.         exact &= (v0 == match && (v1 | match) == 0xff);
  81.     }
  82.     penum->mask_color.mask = mask.all[0];
  83.     penum->mask_color.test = test.all[0];
  84.     penum->mask_color.exact = exact;
  85.     } else {
  86.     penum->mask_color.mask = 0;
  87.     penum->mask_color.test = ~0;
  88.     }
  89.     return image_render_color;
  90. }
  91.  
  92. /* ------ Rendering procedures ------ */
  93.  
  94. /* Test whether a color is transparent. */
  95. private bool
  96. mask_color_matches(const byte *v, const gx_image_enum *penum,
  97.            int num_components)
  98. {
  99.     int i;
  100.  
  101.     for (i = num_components * 2, v += num_components - 1; (i -= 2) >= 0; --v)
  102.     if (*v < penum->mask_color.values[i] ||
  103.         *v > penum->mask_color.values[i + 1]
  104.         )
  105.         return false;
  106.     return true;
  107. }
  108.  
  109. /* Render a color image with 8 or fewer bits per sample. */
  110. private int
  111. image_render_color(gx_image_enum *penum_orig, const byte *buffer, int data_x,
  112.            uint w, int h, gx_device * dev)
  113. {
  114.     const gx_image_enum *const penum = penum_orig; /* const within proc */
  115.     gx_image_clue *const clues = penum_orig->clues; /* not const */
  116.     const gs_imager_state *pis = penum->pis;
  117.     gs_logical_operation_t lop = penum->log_op;
  118.     gx_dda_fixed_point pnext;
  119.     image_posture posture = penum->posture;
  120.     fixed xprev, yprev;
  121.     fixed pdyx, pdyy;        /* edge of parallelogram */
  122.     int vci, vdi;
  123.     const gs_color_space *pcs = penum->pcs;
  124.     cs_proc_remap_color((*remap_color)) = pcs->type->remap_color;
  125.     gs_client_color cc;
  126.     bool device_color = penum->device_color;
  127.     const gx_color_map_procs *cmap_procs = gx_get_cmap_procs(pis, dev);
  128.     cmap_proc_rgb((*map_3)) = cmap_procs->map_rgb;
  129.     cmap_proc_cmyk((*map_4)) =
  130.     (penum->alpha ? cmap_procs->map_rgb_alpha : cmap_procs->map_cmyk);
  131.     bits32 mask = penum->mask_color.mask;
  132.     bits32 test = penum->mask_color.test;
  133.     gx_image_clue *pic = &clues[0];
  134. #define pdevc (&pic->dev_color)
  135.     gx_image_clue *pic_next = &clues[1];
  136. #define pdevc_next (&pic_next->dev_color)
  137.     gx_image_clue empty_clue;
  138.     gx_image_clue clue_temp;
  139.     int spp = penum->spp;
  140.     const byte *psrc_initial = buffer + data_x * spp;
  141.     const byte *psrc = psrc_initial;
  142.     const byte *rsrc = psrc + spp; /* psrc + spp at start of run */
  143.     fixed xrun;            /* x ditto */
  144.     fixed yrun;            /* y ditto */
  145.     int irun;            /* int x/rrun */
  146.     color_samples run;        /* run value */
  147.     color_samples next;        /* next sample value */
  148.     const byte *bufend = psrc + w;
  149.     bool use_cache = spp * penum->bps <= 12;
  150.     int code = 0, mcode = 0;
  151.  
  152.     if (h == 0)
  153.     return 0;
  154.     pnext = penum->dda.pixel0;
  155.     xrun = xprev = dda_current(pnext.x);
  156.     yrun = yprev = dda_current(pnext.y);
  157.     pdyx = dda_current(penum->dda.row.x) - penum->cur.x;
  158.     pdyy = dda_current(penum->dda.row.y) - penum->cur.y;
  159.     switch (posture) {
  160.     case image_portrait:
  161.         vci = penum->yci, vdi = penum->hci;
  162.         irun = fixed2int_var_rounded(xrun);
  163.         break;
  164.     case image_landscape:
  165.         vci = penum->xci, vdi = penum->wci;
  166.         irun = fixed2int_var_rounded(yrun);
  167.         break;
  168.     default:
  169.         break;
  170.     }
  171.  
  172.     if_debug5('b', "[b]y=%d data_x=%d w=%d xt=%f yt=%f\n",
  173.           penum->y, data_x, w, fixed2float(xprev), fixed2float(yprev));
  174.     memset(&run, 0, sizeof(run));
  175.     memset(&next, 0, sizeof(next));
  176.     /* Ensure that we don't get any false dev_color_eq hits. */
  177.     if (use_cache) {
  178.     color_set_pure(&empty_clue.dev_color, gx_no_color_index);
  179.     pic = &empty_clue;
  180.     }
  181.     cs_full_init_color(&cc, pcs);
  182.     run.v[0] = ~psrc[0];    /* force remap */
  183.     while (psrc < bufend) {
  184.     dda_next(pnext.x);
  185.     dda_next(pnext.y);
  186. #define CLUE_HASH3(penum, next)\
  187.   &clues[(next.v[0] + (next.v[1] << 2) + (next.v[2] << 4)) & 255];
  188. #define CLUE_HASH4(penum, next)\
  189.   &clues[(next.v[0] + (next.v[1] << 2) + (next.v[2] << 4) +\
  190.          (next.v[3] << 6)) & 255]
  191.  
  192.     if (spp == 4) {        /* may be CMYK or RGBA */
  193.         next.v[0] = psrc[0];
  194.         next.v[1] = psrc[1];
  195.         next.v[2] = psrc[2];
  196.         next.v[3] = psrc[3];
  197.         psrc += 4;
  198. map4:        if (next.all[0] == run.all[0])
  199.         goto inc;
  200.         if (use_cache) {
  201.         pic_next = CLUE_HASH4(penum, next);
  202.         if (pic_next->key == next.all[0])
  203.             goto f;
  204.         /*
  205.          * If we are really unlucky, pic_next == pic,
  206.          * so mapping this color would clobber the one
  207.          * we're about to use for filling the run.
  208.          */
  209.         if (pic_next == pic) {
  210.             clue_temp = *pic;
  211.             pic = &clue_temp;
  212.         }
  213.         pic_next->key = next.all[0];
  214.         }
  215.         /* Check for transparent color. */
  216.         if ((next.all[0] & mask) == test &&
  217.         (penum->mask_color.exact ||
  218.          mask_color_matches(next.v, penum, 4))
  219.         ) {
  220.         color_set_null(pdevc_next);
  221.         goto mapped;
  222.         }
  223.         if (device_color) {
  224.         (*map_4)(byte2frac(next.v[0]), byte2frac(next.v[1]),
  225.              byte2frac(next.v[2]), byte2frac(next.v[3]),
  226.              pdevc_next, pis, dev,
  227.              gs_color_select_source);
  228.         goto mapped;
  229.         }
  230.         decode_sample(next.v[3], cc, 3);
  231.         if_debug1('B', "[B]cc[3]=%g\n", cc.paint.values[3]);
  232. do3:        decode_sample(next.v[0], cc, 0);
  233.         decode_sample(next.v[1], cc, 1);
  234.         decode_sample(next.v[2], cc, 2);
  235.         if_debug3('B', "[B]cc[0..2]=%g,%g,%g\n",
  236.               cc.paint.values[0], cc.paint.values[1],
  237.               cc.paint.values[2]);
  238.     } else if (spp == 3) {        /* may be RGB */
  239.         next.v[0] = psrc[0];
  240.         next.v[1] = psrc[1];
  241.         next.v[2] = psrc[2];
  242.         psrc += 3;
  243.         if (next.all[0] == run.all[0])
  244.         goto inc;
  245.         if (use_cache) {
  246.         pic_next = CLUE_HASH3(penum, next);
  247.         if (pic_next->key == next.all[0])
  248.             goto f;
  249.         /* See above re the following check. */
  250.         if (pic_next == pic) {
  251.             clue_temp = *pic;
  252.             pic = &clue_temp;
  253.         }
  254.         pic_next->key = next.all[0];
  255.         }
  256.         /* Check for transparent color. */
  257.         if ((next.all[0] & mask) == test &&
  258.         (penum->mask_color.exact ||
  259.          mask_color_matches(next.v, penum, 3))
  260.         ) {
  261.         color_set_null(pdevc_next);
  262.         goto mapped;
  263.         }
  264.         if (device_color) {
  265.         (*map_3)(byte2frac(next.v[0]), byte2frac(next.v[1]),
  266.              byte2frac(next.v[2]),
  267.              pdevc_next, pis, dev,
  268.              gs_color_select_source);
  269.         goto mapped;
  270.         }
  271.         goto do3;
  272.     } else if (penum->alpha) {
  273.         if (spp == 2) {    /* might be Gray + alpha */
  274.         next.v[2] = next.v[1] = next.v[0] = psrc[0];
  275.         next.v[3] = psrc[1];
  276.         psrc += 2;
  277.         goto map4;
  278.         } else if (spp == 5) {    /* might be CMYK + alpha */
  279.         /* Convert CMYK to RGB. */
  280.         frac rgb[3];
  281.  
  282.         color_cmyk_to_rgb(byte2frac(psrc[0]), byte2frac(psrc[1]),
  283.                   byte2frac(psrc[2]), byte2frac(psrc[3]),
  284.                   pis, rgb);
  285.         /*
  286.          * It seems silly to do all this converting between
  287.          * fracs and bytes, but that's what the current
  288.          * APIs require.
  289.          */
  290.         next.v[0] = frac2byte(rgb[0]);
  291.         next.v[1] = frac2byte(rgb[1]);
  292.         next.v[2] = frac2byte(rgb[2]);
  293.         next.v[3] = psrc[4];
  294.         psrc += 5;
  295.         goto map4;
  296.         }
  297.     } else {        /* DeviceN */
  298.         int i;
  299.  
  300.         use_cache = false;    /* should do in initialization */
  301.         if (!memcmp(psrc, run.v, spp)) {
  302.         psrc += spp;
  303.         goto inc;
  304.         }
  305.         memcpy(next.v, psrc, spp);
  306.         psrc += spp;
  307.         if ((next.all[0] & mask) == test &&
  308.         (penum->mask_color.exact ||
  309.          mask_color_matches(next.v, penum, spp))
  310.         ) {
  311.         color_set_null(pdevc_next);
  312.         goto mapped;
  313.         }
  314.         for (i = 0; i < spp; ++i)
  315.         decode_sample(next.v[i], cc, i);
  316. #ifdef DEBUG
  317.         if (gs_debug_c('B')) {
  318.         dprintf2("[B]cc[0..%d]=%g", spp - 1,
  319.              cc.paint.values[0]);
  320.         for (i = 1; i < spp; ++i)
  321.             dprintf1(",%g", cc.paint.values[i]);
  322.         dputs("\n");
  323.         }
  324. #endif
  325.     }
  326.     mcode = remap_color(&cc, pcs, pdevc_next, pis, dev,
  327.                gs_color_select_source);
  328.     if (mcode < 0)
  329.         goto fill;
  330. mapped:    if (pic == pic_next)
  331.         goto fill;
  332. f:    if_debug7('B', "[B]0x%x,0x%x,0x%x,0x%x -> %ld,%ld,0x%lx\n",
  333.           next.v[0], next.v[1], next.v[2], next.v[3],
  334.           pdevc_next->colors.binary.color[0],
  335.           pdevc_next->colors.binary.color[1],
  336.           (ulong) pdevc_next->type);
  337.     /* Even though the supplied colors don't match, */
  338.     /* the device colors might. */
  339.     if (dev_color_eq(*pdevc, *pdevc_next))
  340.         goto set;
  341. fill:    /* Fill the region between */
  342.     /* xrun/irun and xprev */
  343.     switch (posture) {
  344.     case image_portrait:
  345.         {        /* Rectangle */
  346.         int xi = irun;
  347.         int wi =
  348.             (irun = fixed2int_var_rounded(xprev)) - xi;
  349.  
  350.         if (wi < 0)
  351.             xi += wi, wi = -wi;
  352.         if (wi > 0)
  353.             code = gx_fill_rectangle_device_rop(xi, vci, wi, vdi,
  354.                             pdevc, dev, lop);
  355.         xrun = xprev;    /* for sake of final run */
  356.         }
  357.         break;
  358.     case image_landscape:
  359.         {        /* 90 degree rotated rectangle */
  360.         int yi = irun;
  361.         int hi =
  362.             (irun = fixed2int_var_rounded(yprev)) - yi;
  363.  
  364.         if (hi < 0)
  365.             yi += hi, hi = -hi;
  366.         if (hi > 0)
  367.             code = gx_fill_rectangle_device_rop(vci, yi, vdi, hi,
  368.                             pdevc, dev, lop);
  369.         yrun = yprev;    /* for sake of final run */
  370.         }
  371.         break;
  372.     default:
  373.         {        /* Parallelogram */
  374.         code = (*dev_proc(dev, fill_parallelogram))
  375.             (dev, xrun, yrun,
  376.              xprev - xrun, yprev - yrun, pdyx, pdyy,
  377.              pdevc, lop);
  378.         xrun = xprev;
  379.         yrun = yprev;
  380.         }
  381.     }
  382.     if (code < 0)
  383.         goto err;
  384.     rsrc = psrc;
  385.     if ((code = mcode) < 0) {
  386.         /* Invalidate any partially built cache entry. */
  387.         if (use_cache)
  388.         pic_next->key = ~next.all[0];
  389.         goto err;
  390.     }
  391.     if (use_cache)
  392.         pic = pic_next;
  393.     else {
  394.         gx_image_clue *ptemp = pic;
  395.  
  396.         pic = pic_next;
  397.         pic_next = ptemp;
  398.     }
  399. set:    run = next;
  400. inc:    xprev = dda_current(pnext.x);
  401.     yprev = dda_current(pnext.y);    /* harmless if no skew */
  402.     }
  403.     /* Fill the last run. */
  404.     code = (*dev_proc(dev, fill_parallelogram))
  405.     (dev, xrun, yrun, xprev - xrun, yprev - yrun, pdyx, pdyy, pdevc, lop);
  406.     return (code < 0 ? code : 1);
  407.     /* Save position if error, in case we resume. */
  408. err:
  409.     penum_orig->used.x = (rsrc - spp - psrc_initial) / spp;
  410.     penum_orig->used.y = 0;
  411.     return code;
  412. }
  413.